package com.common.atom.seal.util;

import com.alibaba.fastjson2.JSONObject;
import com.common.atom.seal.param.AlphaParam;
import com.common.atom.seal.param.CircularSealParam;
import com.common.atom.seal.param.EllipticSealParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * description: CreateSealUtil <br>
 * date: 2023/4/14  <br>
 * author: zss <br>
 * version: 1.0.0 <br>
 */
public class CreateSealUtil {

    private static final Logger logger = (Logger) LoggerFactory.getLogger(CreateSealUtil.class);

    private static final String ELLIPTIC_SEAL = "财务专用章";
    private static final String CIRCULAR_SEAL = "电子合同章";

    /**
     * 椭圆章
     *
     * @param sealParam
     * @return
     */
    public static byte[] createEllipticSeal(EllipticSealParam sealParam) {
        try {
            checkAlphaValue(sealParam);
            // 获取印章模板
            JSONObject templateRule = SealTemplateDrawUtils.getSealTemplateByTypeName(ELLIPTIC_SEAL);
            if (templateRule == null) {
                logger.error("miss some params.");
                return null;
            }
            BufferedImage bufferedImage = null;
            Map<String, Object> parameters = new HashMap<>();
            parameters.put("content1", sealParam.getCompanyName());
            parameters.put("content2", sealParam.getHorizontalText());
            parameters.put("content3", sealParam.getBottomText());
            parameters.put("color", sealParam.getColor());
            parameters.put("fontType", sealParam.getFontType());

            bufferedImage = SealTemplateDrawUtils.drawSeal(null, templateRule, parameters, null, sealParam.getAlpha());

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            if (sealParam.getDiameter() != null) {
                int width = sealParam.getDiameter();
                int height = sealParam.getDiameter() * 3 / 4;
                BufferedImage buffImg = new BufferedImage(width, height, 6);
                buffImg.getGraphics().drawImage(bufferedImage.getScaledInstance(width, height, 4), 0, 0, null);
                ImageIO.write(buffImg, "png", byteArrayOutputStream);
            } else {
                ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
            }

            return byteArrayOutputStream.toByteArray();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    /**
     * 圆章
     *
     * @param sealParam
     * @return
     */
    public static byte[] makeCircularStamp(CircularSealParam sealParam) {

        try {
            checkAlphaValue(sealParam);
            //获取印章模板
            JSONObject templateRule = SealTemplateDrawUtils.getSealTemplateByTypeName(CIRCULAR_SEAL);
            if (templateRule == null) {
                logger.error("miss some params.");
                return null;
            }
            BufferedImage bufferedImage = null;
            Map<String, Object> parameters = new HashMap<>();
            parameters.put("topName", sealParam.getCompanyName());
            parameters.put("centerName", sealParam.getHorizontalText());
            parameters.put("content3", sealParam.getBottomText());
            parameters.put("color", sealParam.getColor());
            parameters.put("fontType", sealParam.getFontType());

            bufferedImage = SealTemplate.draw(null, templateRule, parameters, null, sealParam.getAlpha());

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            if (sealParam.getDiameter() != null) {
                int width = sealParam.getDiameter();
                int height = sealParam.getDiameter();
                BufferedImage buffImg = new BufferedImage(width, height, 6);
                buffImg.getGraphics().drawImage(bufferedImage.getScaledInstance(width, height, 4), 0, 0, null);
                ImageIO.write(buffImg, "png", byteArrayOutputStream);
            } else {
                ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
            }

            return byteArrayOutputStream.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 检查印章值
     *
     * @param sealParam 印章参数
     */
    private static void checkAlphaValue(AlphaParam sealParam) {
        if (sealParam.getAlpha() < 0) {
            sealParam.setAlpha(0D);
        } else if (sealParam.getAlpha() > 10) {
            sealParam.setAlpha(10D);
        }
    }


}
